package test.poi;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.collections.map.HashedMap;

/**
 * @Description TODO(这里用一句话描述这个类的作用)
 *
 * @author liuqinghua
 * @date 2023-8-24
 */
public class HtmlUtil {
    public static List<String> getImgStr(String htmlStr) {
        List<String> list = new ArrayList<>();
        String img = "";
        Pattern p_image;
        Matcher m_image;
        // String regEx_img = "<img.*src=(.*?)[^>]*?>"; //图片链接地址
        String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
        p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
        m_image = p_image.matcher(htmlStr);
        while (m_image.find()) {
            // 得到<img />数据
            img = m_image.group();
            // 匹配<img>中的src数据
            Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
            while (m.find()) {
                list.add(m.group(1));
            }
        }
        return list;
    }

    public static void main(String[] args) throws IOException {
        File file = new File("D:/git/tag/src/test/java/a1.txt");
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line = null;
        String replaceStr = "";
        while ((line = br.readLine()) != null) {
            replaceStr = line;
            break;
        }
        br.close();
        file = new File("D:/git/tag/src/test/java/aa.txt");
        br = new BufferedReader(new FileReader(file));
        line = null;
        File file2 = new File("D:/git/tag/src/test/java/a2.txt");
        FileWriter bw = new FileWriter(file2);
        Map<String, String> imgMap = new HashMap<String, String>();
        while ((line = br.readLine()) != null) {
            String img = "";
            Pattern p_image;
            Matcher m_image;
            // String regEx_img = "<img.*src=(.*?)[^>]*?>"; //图片链接地址
            String regEx_img = "<img\\s*imgname\\s*=\"[^<]+\"\\s*/>";
            p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
            m_image = p_image.matcher(line);
            while (m_image.find()) {
                // 得到<img />数据
                img = m_image.group();
                // 匹配<img>中的src数据
//                Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
                Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
                Matcher m2 = Pattern.compile("imgname\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
                String imgStr = "";
                String imgName = "";
                if (m.find()) {
                    imgStr = m.group(1);
                }
                if (m2.find()) {
                    imgName = m2.group(1);
                }
                imgMap.put(imgName, imgStr);
                System.out.println(imgName);
            }
            String resultStr = line;
            for (String key : imgMap.keySet()) {
                resultStr = resultStr.replace(imgMap.get(key), replaceStr);
            }
            bw.write(resultStr);
            bw.write("\n");
        }
        bw.flush();
        br.close();
        bw.close();
//        List<String> list = getImgStr(html);
//        System.out.println(list);
    }
}
